package javachess;

public class Bishop extends Piece
{
	public Bishop(int a_owner, int a_x, int a_y)
	{
		super ('b', a_owner);
		m_owner = a_owner;
		m_x = a_x;
		m_y = a_y;
	}
	/**
	 * -Takes in an x and y destination and determines if the location is acceptable.
	 * -Returns a boolean to indicate acceptance.
	 * @param a_x
	 * @param a_y
	 * @return
	 */
	boolean validateMove(Player player, Player otherPlayer, int a_x, int a_y)
	{
		for(int i = 0; i < player.m_pieces.size(); ++i)
		{
			if(player.m_pieces.get(i).m_x == a_x 
			&& player.m_pieces.get(i).m_y == a_y)
			{
				return false;
			}
		}
		
		int SSX = a_x; // SS is the starting, plain x and y are the target
		int SSY = a_y;

		if(!(((SSX - SSY) == (m_x - m_y))
			||
			((SSX + SSY) == (m_x + m_y))))
			return false;
		
		if(SSY < m_y)								// destination is down
		{
			if(SSX < m_x)									// destination is to the right
			{
				for(int mid = 0; mid < m_x - SSX; ++mid)			// make a variable that moves from right start to left finish
					if(mid != 0)
					{
						for(int i = 0; i < player.m_pieces.size(); ++i)
						{
							if(player.m_pieces.get(i).m_x == a_x + mid
							&& player.m_pieces.get(i).m_y == a_y + mid)
							{
								return false;
							}
						}
						for(int i = 0; i < otherPlayer.m_pieces.size(); ++i)
						{
							if(otherPlayer.m_pieces.get(i).m_x == a_x + mid
							&& otherPlayer.m_pieces.get(i).m_y == a_y + mid)
							{
								return false;
							}
						}
					}
			}
			else													// destination is to the left
			{
				for(int mid = 0; mid < m_y - SSY; ++mid)			// make a variable that moves from left start to right finish
					if(mid != 0)					
					{
						for(int i = 0; i < player.m_pieces.size(); ++i)
						{
							if(player.m_pieces.get(i).m_x == a_x + mid
							&& player.m_pieces.get(i).m_y == a_y - mid)
							{
								return false;
							}
						}
						for(int i = 0; i < otherPlayer.m_pieces.size(); ++i)
						{
							if(otherPlayer.m_pieces.get(i).m_x == a_x + mid
							&& otherPlayer.m_pieces.get(i).m_y == a_y - mid)
							{
								return false;
							}
						}
					}
			}
		}
		else													// destination is up
		{
			if(SSX < m_x)									// destination is to the right
			{
				for(int mid = 0; mid < SSY - m_y; ++mid)			// make a variable that moves from right start to left finish
					if(mid != 0)
					{
						for(int i = 0; i < player.m_pieces.size(); ++i)
						{
							if(player.m_pieces.get(i).m_x == a_x - mid
							&& player.m_pieces.get(i).m_y == a_y + mid)
							{
								return false;
							}
						}
						for(int i = 0; i < otherPlayer.m_pieces.size(); ++i)
						{
							if(otherPlayer.m_pieces.get(i).m_x == a_x - mid
							&& otherPlayer.m_pieces.get(i).m_y == a_y + mid)
							{
								return false;
							}
						}
					}
			}
			else													// destination is to the left
			{
				for(int mid = 0; mid < SSX - m_x; ++mid)			// make a variable that moves from right start to left finish
					if(mid != 0)
					{
						for(int i = 0; i < player.m_pieces.size(); ++i)
						{
							if(player.m_pieces.get(i).m_x == a_x - mid
							&& player.m_pieces.get(i).m_y == a_y - mid)
							{
								return false;
							}
						}
						for(int i = 0; i < otherPlayer.m_pieces.size(); ++i)
						{
							if(otherPlayer.m_pieces.get(i).m_x == a_x - mid
							&& otherPlayer.m_pieces.get(i).m_y == a_y - mid)
							{
								return false;
							}
						}
					}
			}
		}

		return true;												// ALL SYSTEMS GO!
		/*
		//these must remain in this scope in order to be reset if a swap occurred
		int temp_x = m_x, temp_y = m_y;
		//if the destination x is less than the object x, swap them
		if(a_x < m_x)
		{
			m_x = a_x;
			a_x = temp_x;
		}
		//if the destination y is less than the object y, swap them
		if(a_y < m_y)
		{
			m_y = a_y;
			a_y = temp_y;
		}
		
		//determine if the bishop moved diagonally
		if((a_x - m_x) == (a_y - m_y))
		{
			//in case a swap occurred
			m_x = temp_x;
			m_y = temp_y;
			for(int i = 0; i < player.m_pieces.size(); ++i)
			{
				//if the location is equal to one of the player's own pieces
				if(player.m_pieces.get(i).m_x == a_x && player.m_pieces.get(i).m_y == a_y )
					return false;
			}
			for(int i = 0; i < otherPlayer.m_pieces.size(); ++i)
			{
				//if the location is equal to another player's piece
				if(otherPlayer.m_pieces.get(i).m_x == a_x && otherPlayer.m_pieces.get(i).m_y == a_y )
					return true;
			}
			return true;
		}
		//in case a swap occurred
		m_x = temp_x;
		m_y = temp_y;
		return false;
		*/
	}
}
